﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MadeInPortugal;

namespace MadeInPortugalWebApplication.WebControl
{
    public partial class UserPass : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if ((Session["UserId"] == null) || (Session["UserType"] == null))
            {
                login.Visible = true;
                LoggedIn.Visible = false;
            }
            else
            {
                login.Visible = false;
                LoggedIn.Visible = true;
                lblUserName.Text = Session["UserName"].ToString();
            }
            if (!IsPostBack)
            {
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            EnterpriseUser logedUser = new EnterpriseUser();
            logedUser.User = Tbxuser.Text;
            logedUser.Password = TbxPassword.Text;
            EnterpriseUser loged = new EnterpriseUser();
            loged = logedUser.SignIn(logedUser);
            if (logedUser.Id != 0)
            {
                string EnterpriseId = logedUser.Id.ToString();
                Session["UserId"] = logedUser.Id.ToString();
                Session["UserType"] = logedUser.UserType;
                Enterprise enterp = new Enterprise();
                enterp = enterp.Get(loged.Id);
                Session["UserName"] = enterp.Name;
                login.Visible = false;
                LoggedIn.Visible = true;
                lblUserName.Text = Session["UserName"].ToString();
                if (logedUser.UserType == "Admin")
                    Response.Redirect("AdminProductList.aspx");
                else
                    Response.Redirect("Index.aspx");
            }
            else
                PasswordValidator.IsValid = false;

        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            //Session["UserName"] = null;
            //Session["UserId"] = null;
            //Session["UserType"] = null;
            //Session["Acordeon"] = null;
            Session.Clear();
            login.Visible = true;
            LoggedIn.Visible = false;
            Response.Redirect("index.aspx");
        }
    }
}